package com.coder520.common.task;

import com.coder520.common.Const;
import com.coder520.dao.AttendMapper;
import com.coder520.dao.UserMapper;
import com.coder520.pojo.Attend;
import com.coder520.pojo.User;
import com.coder520.util.DateUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ASUS on 2017/7/7.
 */
public class AttendCheckTask {

    private static Logger logger = LoggerFactory.getLogger(AttendCheckTask.class);

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm;ss");

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AttendMapper attendMapper;

    public void checkAttend() {
        //查询没有打卡的人
        List<Long> userList = userMapper.selectNoColum();

        Date now = new Date();
        //if (CollectionUtils.isNotEmpty(userList)) {
            List<Attend> attendList = Lists.newArrayList();
            for (Long  id : userList) {
                Attend attend = new Attend();
                attend.setAttendDate(now);
                attend.setAttendStatus(Const.NOT_NORMAL);
                attend.setAbsence(480);
                attend.setUserId(id);
                attend.setAttendWeek((byte) DateUtil.getTodayWeek());

                attendList.add(attend);
            }
            attendMapper.batchInsert(attendList);
        //}
        //检查有打卡记录的人
        List<Attend> attendList2 = attendMapper.selectColum();
        if(CollectionUtils.isNotEmpty(attendList)){
            for (Attend attend : attendList2) {
                attend.setAttendStatus(Const.NOT_NORMAL);
                attend.setAbsence(480);
                attendMapper.updateByPrimaryKeySelective(attend);
            }

        }


    }
}
